// 权限控制
// 这里编写路由权限控制代码
import { getToken } from '@/utils/auth'
import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
const WHITELIST = ['/login', '/404'] // 白名单

router.beforeEach((to, from, next) => {
  // 开启进度条
  NProgress.start()
  const token = getToken()
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/fetchUserInfo')
      }
    }
  } else {
    if (WHITELIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  // 技术进度条
  NProgress.done()
})
